<?php

include_once WP_PLUGIN_DIR . '/epart-common/common-util.php';

/**
 * Epart BBS Plugin Widget Class
 *
 * @package epartbbs
 */
class WP_Widget_Schedule extends WP_Widget {

    /** constructor */
    function WP_Widget_Schedule() {
        $widget_ops = array('classname' => 'widget_'.EPART_SCHEDULE_NAME, "description" => "일정플러그인 위젯 추출");
        parent::WP_Widget(EPART_SCHEDULE_NAME."Widget", $name = EPART_SCHEDULE_NAME.'Widget', $widget_ops);

        if(basename($_SERVER["PHP_SELF"]) == "widgets.php"){
            wp_enqueue_style(EPART_SCHEDULE_NAME."_admin_css", constant(EPART_SCHEDULE_NAME_U."_PLUGIN_CSS_URL") . "/".EPART_SCHEDULE_NAME."-admin.css");
        }

        if(!is_admin()){
            //wp_enqueue_style(EPART_SCHEDULE_NAME."_user_tabs_css", constant(EPART_SCHEDULE_NAME_U."_PLUGIN_CSS_URL") . "/".EPART_SCHEDULE_NAME."_user_tabs.css");
            //wp_enqueue_script(EPART_SCHEDULE_NAME."_user_tabs_script_handle", constant(EPART_SCHEDULE_NAME_U."_PLUGIN_JS_URL") . "/".EPART_SCHEDULE_NAME."_user_tabs.js", array("jquery"));
        }
    }

    /** @see WP_Widget::widget */
    function widget($args, $instance) {
       extract($args);
       //$title = apply_filters('widget_title', $instance['title']);
       $title = apply_filters('widget_title', empty($instance['title']) ? '' : $instance['title'], $instance, $this->id_base);
       $option1 = apply_filters('widget_option1', empty($instance['option1']) ? '' : $instance['option1'], $instance, $this->id_base);
       $option_tab1 = apply_filters('widget_option_tab1', empty($instance['option_tab1']) ? '' : $instance['option_tab1'], $instance, $this->id_base);
       $widget_id = apply_filters('widget_id', empty($instance['widget_id']) ? '' : $instance['widget_id'], $instance, $this->id_base);

       $option_tab1_arr = array();
       $option_tab1_arr[0] = "test1";
       $option_tab1_arr[1] = "test2";
       $option_tab1_arr[2] = "test3";
?>
        <?php echo $before_widget; ?>
        <?php if ($title)
                    echo $before_title . $title . $after_title; ?>

        <div id="main_tab_<?php echo $widget_id;?>" style="padding:0px;">
            옵션1 : <?php echo $option1;?><br />
            옵션 탭1 : <?php echo $option_tab1_arr[$option_tab1];?>
        </div>

        <script type="text/javascript">initTabClickMenu("main_tab_<?php echo $widget_id;?>");</script>
        <?php echo $after_widget; ?>
<?php
    }

    /** @see WP_Widget::update */
    function update($new_instance, $old_instance) {
        $instance = $old_instance;
        $instance['title'] = strip_tags($new_instance['title']);
        $instance['option1'] = strip_tags($new_instance['option1']);
        $instance['option_tab1'] = strip_tags($new_instance['option_tab1']);
        $instance['widget_id'] = strip_tags($new_instance['widget_id']);

        return $instance;
    }

    /** @see WP_Widget::form */
    function form($instance) {
        $title = "";
        if (isset($instance['title'])) {
            $title = esc_attr($instance['title']);
        }
        $option1 = "";
        if (isset($instance['option1'])) {
            $option1 = esc_attr($instance['option1']);
        }
        $option_tab1 = array();
        if (isset($instance['option_tab1'])) {
            $option_tab1 = esc_attr($instance['option_tab1']);
        }
        $widget_id = "";
        if (isset($instance['widget_id'])) {
            $widget_id = esc_attr($instance['widget_id']);
        }

        $option_tab1_arr = array();
        $option_tab1_arr[0] = "test1";
        $option_tab1_arr[1] = "test2";
        $option_tab1_arr[2] = "test3";
?>
        <p>
            <input type="hidden" id="<?php echo $this->get_field_id('widget_id'); ?>" name="<?php echo $this->get_field_name('widget_id'); ?>" value="<?php echo rand(0,100000);?>">
            <label for="<?php echo $this->get_field_id('title'); ?>"><?php _e('Title:'); ?></label>
            <input class="widefat" id="<?php echo $this->get_field_id('title'); ?>" name="<?php echo $this->get_field_name('title'); ?>" type="text" value="<?php echo $title; ?>" /><br /><br />
            <label for="<?php echo $this->get_field_id('option1'); ?>"><?php _e('옵션1:'); ?></label>
            <input class="widefat" id="<?php echo $this->get_field_id('option1'); ?>" name="<?php echo $this->get_field_name('option1'); ?>" type="text" value="<?php echo $option1; ?>" /><br /><br />
            <label for="<?php echo $this->get_field_id('option_tab1'); ?>"><?php _e('1번 탭: '); ?></label>
            <select id="<?php echo $this->get_field_id('option_tab1'); ?>" name="<?php echo $this->get_field_name('option_tab1');?>" >
                <option value="" selected>선택해주세요</option>
                <?php echo ep_get_options($option_tab1_arr, $option_tab1);?>
            </select>            
        </p>

<?php
    }
}
?>
